<template>
	<view>
		<view class="search"><my-search @click="goSearch"></my-search></view>
		<!-- 轮播图 -->
		<swiper
			:indicator-dots="true"
			:autoplay="true"
			:interval="3000"
			:duration="1000"
			circular="true"
		>
			<swiper-item v-for="(item, index) in swiperList" :key="index">
				<navigator
					class="swiper-item"
					:url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`"
				>
					<img :src="item.image_src" alt="" />
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 分类导航 -->
		<view class="nav-list">
			<view
				class="nav-item"
				v-for="(item, index) in navList"
				:key="index"
				@click="navClickHandler(item)"
			>
				<image :src="item.image_src" class="nav-img"></image>
			</view>
		</view>
		<!-- 楼层信息 -->
		<view class="floor-list" v-for="(item, index) in floorList" :key="index">
			<!-- 标题 -->
			<image :src="item.floor_title.image_src" class="floor-title"></image>
			<!-- 内容 -->
			<view class="floor-item">
				<navigator class="item-left" :url="item.product_list[0].url">
					<image
						:src="item.product_list[0].image_src"
						mode="widthFix"
						:style="{ width: item.product_list[0].image_width + 'rpx' }"
					></image>
				</navigator>
				<view class="item-right">
					<navigator
						class="item-right-box"
						v-for="(i, d) in item.product_list"
						:key="d"
						v-if="d !== 0"
						:url="i.url"
					>
						<image
							:src="i.image_src"
							mode="widthFix"
							:style="{ width: i.image_width + 'rpx' }"
						></image>
					</navigator>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import badgeMix from '@/mixins/tabbar-badeg.js';
export default {
	mixins: [badgeMix],
	data() {
		return {
			swiperList: [],
			navList: [],
			floorList: []
		};
	},
	onLoad() {
		this.getSwiperList();
		this.getNavList();
		this.getFloorList();
	},
	methods: {
		// 获取轮播图
		async getSwiperList() {
			const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata');
			if (res.meta.status !== 200) return uni.$showMsg();
			this.swiperList = res.message;
		},
		// 获取分类导航
		async getNavList() {
			const { data: res } = await uni.$http.get('/api/public/v1/home/catitems');
			if (res.meta.status !== 200) return uni.$showMsg();
			this.navList = res.message;
			// console.log(this.navList);
		},
		// nav-item项被点击时候的事件处理函数
		navClickHandler(item) {
			// 判断点击的是哪个 nav
			if (item.name === '分类') {
				uni.switchTab({
					url: '/pages/cate/cate'
				});
			}
		},
		// 获取楼层信息
		async getFloorList() {
			const { data: res } = await uni.$http.get('/api/public/v1/home/floordata');
			if (res.meta.status !== 200) return uni.$showMsg();
			res.message.forEach(floor => {
				floor.product_list.forEach(item => {
					item.url = '/subpkg/goods_list/goods_list?' + item.navigator_url.split('?')[1];
				});
			});
			this.floorList = res.message;
			console.log(this.floorList);
		},
		// 跳转搜索界面
		goSearch() {
			uni.navigateTo({
				url: '/subpkg/search/search'
			});
		}
	}
};
</script>

<style lang="scss" scoped>
// 搜索
.search {
	position: sticky;
	top: 0;
	z-index: 999;
}
// 轮播图
swiper {
	height: 330rpx;
	.swiper-item,
	image {
		width: 100%;
		height: 100%;
	}
}
// 分类导航
.nav-list {
	display: flex;
	justify-content: space-around;
	margin: 15px 0;
	.nav-item {
		.nav-img {
			width: 128rpx;
			height: 140rpx;
		}
	}
}
// 楼层信息
.floor-list {
	.floor-title {
		height: 60rpx;
		width: 100%;
		display: flex;
	}
	.floor-item {
		display: flex;
		padding-left: 10rpx;
		.item-right {
			padding: 0 10rpx;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
		}
	}
}
</style>
